/* TCSS 590A - Cybernetics and Systems
 * Group Project - Modeling
 * By Barry Jones, Naren Meadem, Michael Pitts, Yuan Tao
 * Fall 2012, University of Washington, Tacoma
 */
package model.delayed;

import java.util.Collection;


/**
 * A model controller that does not expect delays.
 * @author Michael Pitts
 * @version Nov 19, 2012
 */
public class SimpleModelController implements ModelController
{
	/** Is the savings savingsAccount for the model.*/
	private final Account my_saving;
	
	/** Is the checking savingsAccount for the model.*/
	private final Account my_checking;
	
	/**we have saving account keeping track of all the details*/
	
	

	/**
	 * Creates a new SimpleModelController, which does not expect any delays.
	 * @param the_saving is the savings savingsAccount for the model.
	 * @param the_checking is the checking savingsAccount for the model.
	 */
	public SimpleModelController(final Account the_saving, final Account the_checking)
	{
		my_saving = the_saving;
		my_checking = the_checking;
	}

	@Override public Collection<Event> compute(long the_time, 
			final Collection<Event> the_events)
	{
		//double my_percent = .1;
		long net = totalTo(the_events);
		if (net < 0)
		{
			the_events.add(my_saving.withdraw(net, the_time));
			the_events.add(my_checking.deposit(-net, the_time));
		} else if (net > 0)
		{
			
			the_events.add(my_saving.deposit(net, the_time));
			the_events.add(my_checking.withdraw(-net, the_time));
		}
		/*sarting here
		//while (my_checking.getValue() < my_saving.getValue()*.10)
		if (my_checking.getValue() < my_saving.getValue()*.1 )
		{
			//System.out.println("burp");
			the_events.add(my_saving.withdraw((long) -((my_saving.getValue()*my_percent )-my_checking.getValue()), the_time));
			the_events.add(my_checking.deposit((long)  ((my_saving.getValue()*my_percent )-my_checking.getValue()), the_time));
		}
		if (my_checking.getValue() > my_saving.getValue()*.1 )
		{
			//System.out.println("burp");
			the_events.add(my_saving.deposit((long) (my_checking.getValue()-(my_saving.getValue()*my_percent)), the_time));
			the_events.add(my_checking.withdraw((long) -(my_checking.getValue()-(my_saving.getValue()*my_percent )), the_time));
		}*/
		
		return the_events;
	}
	
	/**
	 * Returns the sum of all values for all events with time <= the_time.
	 * @param the_time the time to sum values to.
	 * @param the_events are the most recent events to consider.
	 * @return an integer of any sign.
	 */
	private long totalTo(final Collection<Event> the_events)
	{
		long total = 0;
		for (Event e : the_events) total += e.value;
		return total;
	}
}
